package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/*
    studentServlet用来处理对学生进行增删改查的请求
    处理分页查询的请求:
    处理添加学生的请求:
    处理修改学生的请求:
    处理删除学生的请求:

    问题:一个Servlet如何区分四种请求?
    解决:不同的请求都带一个不同的标记(method)参数过来就行了.然后根据标记调用不同的方法就行了
 */
@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {

    private StudentService studentService;

    @Override
    public void init() throws ServletException {
        super.init();
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        studentService = ac.getBean("studentService", StudentService.class);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决请求和响应中文乱码问题
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        //获取标记
        String method = request.getParameter("method");
        //判断标记并且调用对应的方法处理请求
        if("selectByPage".equals(method)){
            //调用分页查询的方法
            selectByPage(request,response);
        }else if("addStudent".equals(method)){
            //调用添加学生的方法
            addStudent(request,response);
        }else if("updateStudent".equals(method)){
            //调用修改学生的方法
            updateStudent(request,response);
        }else if("deleteStudent".equals(method)){
            //调用删除学生的方法
            deleteStudent(request,response);
        }
    }
    //处理删除学生的请求
    private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1 获取请求参数
        String number = request.getParameter("number");

        //2 调用service删除学生信息，获取影响的行数
        int count = studentService.deleteStudent(number);

        //3 响应结果
        response.getWriter().write(String.valueOf(count));
    }

    //处理修改学生的请求
    private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1 获取请求参数并封装
        Student student=new Student();
        try {
            BeanUtils.populate(student,request.getParameterMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //2 调用service层方法,修改学生信息
        int count = studentService.updateStudent(student);

        //3 响应结果
        response.getWriter().write(String.valueOf(count));
    }

    //处理添加学生的请求
    private void addStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1 获取请求参数并封装
        Student student=new Student();
        try {
            BeanUtils.populate(student,request.getParameterMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //2 调用service层方法,添加学生信息
        int count = studentService.addStudent(student);

        //3 响应结果
        response.getWriter().write(String.valueOf(count));
    }

    //处理分页查询的方法
    private void selectByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1 获取请求参数(currentPage和pageSize)
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");

        //2 直接调用service层方法,获取分页结果pageInfo
        PageInfo<Student> pageInfo = studentService.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));

        //3 将pageInfo转换成json响应给客户端
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.writeValue(response.getWriter(),pageInfo);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
